package com.team.easybuy.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.easybuy.entity.Category;
import com.team.easybuy.entity.ProductCategory;
import com.team.easybuy.service.ProductCategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cate")
public class CateController {

    @Resource
    private ProductCategoryService productCategoryService;

    @RequestMapping("/PageByAllCate")
    public PageInfo<Category> PageByAllCate(@RequestParam Map<String, Object> params) {
        int pageNum = Integer.parseInt(params.get("pageNum").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);
        List<Category> list = productCategoryService.listAllCate();
        return new PageInfo<>(list, 5);
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        return productCategoryService.deleteById(id);
    }

    @RequestMapping("/loadCateOne")
    public List<ProductCategory> loadCateOne() {
        return productCategoryService.loadCateWhich(0, 1);
    }

    @RequestMapping("/loadCateTwo/{id}")
    public List<ProductCategory> loadCateTwo(@PathVariable Integer id) {
        return productCategoryService.loadCateWhich(id, 2);
    }

    @RequestMapping("/addCate")
    public String addCate(@RequestBody Map<String, Object> params) {
        return productCategoryService.addCate(params);
    }

    @RequestMapping("/editCateById")
    public Category editCateById(Integer id, Integer type) {
        return productCategoryService.loadCateOne(id, type);
    }

    @RequestMapping("/editCate")
    public String editCate(@RequestBody Map<String, Object> params) {
        return productCategoryService.editCate(params);
    }

    @RequestMapping("/loadCateThree/{id}")
    public List<ProductCategory> loadCateThree(@PathVariable Integer id) {
        return productCategoryService.loadCateWhich(id, 3);
    }
}
